﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LIMS.Entities;
using LIMS.DataAccess;
using System.Collections;
using System.Reflection;
using Castle.Core.Logging;
using NHibernate.Criterion;

namespace LIMS.Service
{
    public class PlantManagementService : IPlantManagementService
    {
        //Private DBA interfaces
        private IDaoFactory oDBFactory;
        private IPlantDAO oPlantDBA;
        private ILogger logger;
        private ISiteManagementService siteService;
        
        //Constructor
        public PlantManagementService(IDaoFactory oDBOp, ILogger oLogger, ISiteManagementService siteService)
        {
            this.oDBFactory = oDBOp;
            this.oPlantDBA = oDBFactory.GetPlantDAO();
            this.logger = oLogger;
            this.siteService = siteService;
        }
        #region IPlantManagementService Members

        public List<Plant> GetAllPlants()
        {
            List<Plant> myPlants = new List<Plant>();// oPlantDBA.GetAll();


            try
            {
                ICriterion oCriteria = Expression.Eq("Status", Resource.ACTIVE_STATUS);
                myPlants = oPlantDBA.GetByCriteria(oCriteria);
            }
            catch (Exception ex)
            {
                logger.Error("Error while getting all user Plant.", ex);
                //    return null;
            }
            return myPlants;
        }

        public bool Save(Plant newPlant)
        {
            return oPlantDBA.QuickSaveOrUpdate(ref newPlant);
        }

        public bool Delete(Plant dPlant)
        {
            return oPlantDBA.QuickDelete(dPlant);
        }

        public List<Plant> GetAllPlantsBySiteId(int iSiteId)
        {
            throw new NotImplementedException();
        }


        /// <summary>
        /// GetAllSitesForGrid
        /// </summary>
        /// <param name="oGrdData"></param>
        /// <returns></returns>
        public List<Plant> GetAllPlantForGrid(ref GridViewData oGrdData)
        {
            List<Plant> plantList = new List<Plant>();
            plantList=oPlantDBA.GetAllForGridView(ref oGrdData);

            foreach (Plant oPlant in plantList)
            {
                oPlant.SiteName = siteService.GetSiteNameByID(oPlant.SiteId);

            }
            return plantList;
        }


        /// <summary>
        /// GetPlantNameByID
        /// </summary>
        /// <param name="plantid"></param>
        /// <returns></returns>
        public string GetPlantNameByID(int plantid)
        {
            string plantName = string.Empty;
            List<Plant> oPlantList = new List<Plant>();
            try
            {

                ICriterion oCriteriaStatus = Expression.Eq("Status", Resource.ACTIVE_STATUS);
                ICriterion oCriteriaID = Expression.Eq("Id", plantid);
                ICriterion oFinalCriteria = Expression.And(oCriteriaStatus, oCriteriaID);
                oPlantList = oPlantDBA.GetByCriteria(oFinalCriteria);
                if (oPlantList.Count > 0)
                {
                    foreach (Plant oPlant in oPlantList)
                    {
                        plantName = oPlant.Name;
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("error while getting plant name by id", ex);
            }
            return plantName;


        }

        public List<Plant> GetPlantsBySite(string sSiteId)
        {
            List<Plant> myPlants = new List<Plant>();


            try
            {
                string[] aSiteId =  new string[0];
                aSiteId = sSiteId.Split(Convert.ToChar(","));
                ICriterion oCriteriaSiteId = Expression.In("SiteId", aSiteId);
                ICriterion oCriteriaStatus = Expression.Eq("Status", Resource.ACTIVE_STATUS);
                ICriterion oCriteria = Expression.And(oCriteriaSiteId, oCriteriaStatus);
                myPlants = oPlantDBA.GetByCriteria(oCriteria);
            }
            catch (Exception ex)
            {
                logger.Error("Error while getting plants by site.", ex);
                //    return null;
            }
            return myPlants;
        }

        public List<Plant> GetPlantsBySite(int iSiteId)
        {
            List<Plant> myPlants = new List<Plant>();


            try
            {
               ICriterion oCriteriaSiteId = Expression.Eq("SiteId", iSiteId);
                ICriterion oCriteriaStatus = Expression.Eq("Status", Resource.ACTIVE_STATUS);
                ICriterion oCriteria = Expression.And(oCriteriaSiteId, oCriteriaStatus);
                myPlants = oPlantDBA.GetByCriteria(oCriteria);
            }
            catch (Exception ex)
            {
                logger.Error("Error while getting plants by site.", ex);
                //    return null;
            }
            return myPlants;
        }

        #endregion
    }
}
